//
//  WHWelcomeController.swift
//  microblog
//
//  Created by Apple on 15/11/28.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class WHWelcomeController: UIViewController {

    //5，iconView底部约束
    private var iconViewBottomCon: NSLayoutConstraint?
    
    //7 .在用户看得到View的时候开始做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //执行
        moveAnimation()
    }
    
    //6. 制作动画
    private func moveAnimation() {
        // 使用autolayout布局之后,不要去修改view的frame,bounds,center,修改约束的值就可以了
        iconViewBottomCon?.constant = 160 - UIScreen.mainScreen().bounds.height
        //动画
        // 弹簧效果动画
        // duration: 1.2
        // delay: 延时
        // usingSpringWithDamping: 弹簧效果明显程度 0 - 1
        // initialSpringVelocity: 初始速度
        UIView.animateWithDuration(1.2, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //更新布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //print("终于完成了")
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        
    }
    // 4 UI
    private func prepareUI() {
        //添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        //约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        // 背景,填充父控件
        // VFL(可视化格式语言),作用:也是使用autolayou来约束控件比较形象,简化autolayout的代码约束
        // 创建约束,通过类方法
        // NSLayoutConstraintconstraintsWithVisualFormat   返回的是约束的数组
        // format: VFL约束
        // options 默认
        // metrics: 默认
        // views: 参照映射
        /*
        VFL:
        H: 水平方向
        V: 垂直方向
        | 父控件的边界
        [] 表示view
        */
        
        //左边和右边距离父控件都为0
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[biv]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bkgImageView]))
        //顶部和底部距离父控件都为0
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bkgImageView]))
        //头像
        //顶部和底部距离父控件都为0
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //做动画
        iconViewBottomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottomCon!)
        
        
        //信息
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //底部和顶部
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))


    }
    
    //1懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //2头像
    private lazy var iconView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆角
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        return view
    }()
    //3 信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        return label
    }()
    
}
